from datetime import datetime
from flask import render_template, session, redirect, url_for, flash

from . import main
from .forms import NameForm


@main.route('/', methods={'GET', 'POST'})
def index():
    form = NameForm()
    if form.validate_on_submit():
        old_name = session.get('name')
        if old_name is not None and old_name != form.name.data:
            flash('Looks like you have changed your name!')
        session['name'] = form.name.data
        return redirect(url_for('.index'))
    return render_template('index.html',
                           form=form,
                           name=session.get('name'),
                           known=session.get('known', False),
                           current_time=datetime.utcnow())


@main.route('/user/<name>')
@main.route('/user/<name>/')
def user(name):
    return render_template('user.html', name=name)


@main.route('/user/agent')
def user_agent():
    return render_template('user_agent.html', user_agent=user_agent)